package com.hgd.security.evp.biz.biz;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.hgd.security.evp.biz.entity.InspectionSituation;
import com.hgd.security.evp.biz.mapper.InspectionSituationMapper;
import com.hgd.security.evp.common.biz.BusinessBiz;
import com.hgd.security.evp.common.util.EmptyUtil;
import com.hgd.security.evp.common.util.ObjectResultResponse;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 
 *
 * @author By Shang
 * @email ${email}
 * @version 2019-03-25 22:23:09
 */
@Service
public class InspectionSituationBiz extends BusinessBiz<InspectionSituationMapper, InspectionSituation> {


    public ObjectResultResponse<Void> addInspectionSitt(InspectionSituation inspectionSituation){
        ObjectResultResponse<Void> restResult=new ObjectResultResponse<>();

        this.insertSelectiveBindCrt(inspectionSituation);

        restResult.setStatus(200);
        restResult.setMessage("添加记录成功");
        return restResult;
    }

    /**
     * 获取最新数据
     * @return
     */
    public JSONObject lastestSituation() {
        Example example=new Example(InspectionSituation.class)
            .selectProperties("id","actualConcentration");

        example.setOrderByClause("id desc");
        PageHelper.startPage(1, 50);
        List<InspectionSituation> inspectionSituations = this.selectByExample(example);
        Collections.reverse(inspectionSituations);

        JSONObject result=new JSONObject();
        List<String> xAxis=new ArrayList<>();
        List<Double> series=new ArrayList<>();
        if(EmptyUtil.isNotEmpty(inspectionSituations)){
            inspectionSituations.forEach(inspectionSituation -> {
                xAxis.add("");
                series.add(inspectionSituation.getActualConcentration()/100);
            });

            result.put("xAxis", xAxis);
            result.put("series", series);
        }

        return result;
    }
}